import time
from unittest import expectedFailure

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver

class LoginTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/login.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)

    #登录失败
    def loginFailTest(self):
        #查找 用户名 密码 登录按钮
        username = self.driver.find_element(By.XPATH, "//*[@id=\"username\"]")
        passwd = self.driver.find_element(By.XPATH, "//*[@id=\"password\"]")
        button = self.driver.find_element(By.XPATH, "//*[@id=\"submit\"]")

        #用户名为空
        username.clear()
        passwd.clear()
        passwd.send_keys("123")
        button.click()
        #警告弹窗
        self.driver.switch_to.alert.accept()

        # 密码为空
        username.clear()
        passwd.clear()
        username.send_keys("lisi")
        button.click()
        # 警告弹窗
        self.driver.switch_to.alert.accept()

        # 用户名和密码为空
        username.clear()
        passwd.clear()
        button.click()
        # 警告弹窗
        self.driver.switch_to.alert.accept()

        # 用户名和密码错误
        username.clear()
        passwd.clear()
        username.send_keys("lisi1")
        passwd.send_keys("1233")
        button.click()
        #等待弹窗出现，显式等待
        wait = WebDriverWait(self.driver, 2)
        alter = wait.until(expected_conditions.alert_is_present())
        alter.accept()

    #登录成功
    def loginSucTest(self, user):
        #输入正确的用户名和密码，登录成功
        self.driver.find_element(By.XPATH, "//*[@id=\"username\"]").clear()
        self.driver.find_element(By.XPATH, "//*[@id=\"password\"]").clear()
        self.driver.find_element(By.XPATH, "//*[@id=\"username\"]").send_keys(user)
        self.driver.find_element(By.XPATH, "//*[@id=\"password\"]").send_keys("123")
        self.driver.find_element(By.XPATH, "//*[@id=\"submit\"]").click()
        #登录成功后会有一个弹窗
        # 等待弹窗出现，显式等待
        wait = WebDriverWait(self.driver, 2)
        alter = wait.until(expected_conditions.alert_is_present())
        alter.accept()
        # 截图
        ChatDriver.getScreenShot()